package jltx.fragmentpageframework.widget;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import cn.jltx.utils.XqLog;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/22.
 */

public class BottomBarV2 extends LinearLayout{
    private LayoutParams mTabParams;
    private int mCurrentPosition = 0;

    private OnTabSelectedListener onTabSelectedListener;

    public BottomBarV2(Context context) {
        super(context);
        initView();
    }

    public BottomBarV2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BottomBarV2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        setOrientation(HORIZONTAL);
        mTabParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        mTabParams.weight = 1;
    }

    public BottomBarV2 addItem(final BottomBarTabV2 tab) {
        tab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTabSelectedListener == null) return;
                int pos = tab.getTabPosition();
                if (mCurrentPosition == pos) {
                    onTabSelectedListener.onTabReselected(pos);
                } else {
                    tab.setSelected(true);
                    onTabSelectedListener.onTabUnselected(mCurrentPosition);
                    getChildAt(mCurrentPosition).setSelected(false);
                    onTabSelectedListener.onTabSelected(pos, mCurrentPosition);
                    mCurrentPosition = pos;
                }
            }
        });
        tab.setTabPosition(this.getChildCount());
        tab.setLayoutParams(mTabParams);
        addView(tab);
        if(mCurrentPosition == tab.getTabPosition()){
            tab.setSelected(true);
        }else {
            tab.setSelected(false);
        }
        return this;
    }

    /**
     * 保存索引
     * @return
     */
    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable supterState = super.onSaveInstanceState();
        return new SaveState(supterState,mCurrentPosition);
    }

    /**
     * 恢复索引
     * @param state
     */
    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SaveState ss = (SaveState)state;
        super.onRestoreInstanceState(ss.getSuperState());
        if(mCurrentPosition != ss.position){
            if(mCurrentPosition >=0 && mCurrentPosition < getChildCount()){
                getChildAt(mCurrentPosition).setSelected(false);
            }

            if(ss.position >=0 && ss.position < getChildCount()){
                getChildAt(ss.position).setSelected(true);
            }

        }
        mCurrentPosition = ss.position;
    }

    public void onTabPageScrolled(int curPos,int nextPos,float positionOffset){
        if(positionOffset > 0){
            BottomBarTabV2 curTab = (BottomBarTabV2)getChildAt(curPos);
            BottomBarTabV2 nextTab = (BottomBarTabV2)getChildAt(nextPos);
            curTab.changeBottomBatTabColor(1 - positionOffset);
            nextTab.changeBottomBatTabColor(positionOffset);
        }
    }
    public void setOnTabSelectedListener(OnTabSelectedListener onTabSelectedListener) {
        this.onTabSelectedListener = onTabSelectedListener;
    }

    public void setCurrentPosition(int position){
        if(this.mCurrentPosition != position){
            this.mCurrentPosition = position;
            resetOtherTabs();
            getChildAt(mCurrentPosition).setSelected(true);
            XqLog.d("YYYY","setCurrentPosition.true:"+position);
        }
    }

    /**
     * 重置其他的TabIndicator的颜色
     */
    private void resetOtherTabs()
    {
        for (int i = 0; i < this.getChildCount(); i++)
        {
            if(mCurrentPosition != i){
                getChildAt(i).setSelected(false);
                XqLog.d("YYYY","setCurrentPosition.false:"+i);
            }
        }
    }

    public interface OnTabSelectedListener {
        void onTabSelected(int position, int prePosition);
        void onTabUnselected(int position);
        void onTabReselected(int position);
    }

    static class SaveState extends BaseSavedState{
        private int position;

        public SaveState(Parcel source) {
            super(source);
            this.position = source.readInt();
        }

        public SaveState(Parcelable superState, int position) {
            super(superState);
            this.position = position;
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(position);
        }

        public static final Creator<BottomBar.SaveState> CREATOR = new Creator<BottomBar.SaveState>() {
            @Override
            public BottomBar.SaveState createFromParcel(Parcel source) {
                return new BottomBar.SaveState(source);
            }

            @Override
            public BottomBar.SaveState[] newArray(int size) {
                return new BottomBar.SaveState[size];
            }
        };

    }
}
